package com.ybkj.daijia.api.apirest;

import com.ybkj.daijia.serverUtils.HttpClientUtil;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.sales.ActivityManage;
import com.ybkj.daijia.server.sales.ActivityManage.ServiceType;
import com.ybkj.daijia.server.service.ActivityManageService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.CouponRuleService;
import com.ybkj.daijia.server.service.MemberService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("ApiActivityManageProcessController")
@RequestMapping(value = "api/activitymanage")
public class ActivityManageProcessController {

    @Autowired
    private ActivityManageService activityManageService;
    @Autowired
    private CouponRuleService couponRuleService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private MemberService memberService;


    @RequestMapping(value = "queryActivityManages")
    public @ResponseBody
    Map<String, Object> queryActivitys(
        @RequestParam(required = true, value = "longitude") Double longitude,
        @RequestParam(required = true, value = "latitude") Double latitude,
        @RequestParam(required = false, value = "passengerId") Long passengerId,
        @RequestParam(required = false, value = "acKey") String acKey,
        Integer page, Integer limit) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);
        Long companyId = null;
        if (StringUtils.isNotBlank(acKey)) {
            Company company = companyService.findByAppKey(acKey);
            if (company != null) {
                companyId = company.getId();
            }
        } else if (longitude != null && latitude != null) {
            String city = HttpClientUtil.getCityByLngAndLat(longitude, latitude);
            List<Company> companyList = null;
            if (StringUtils.isNotBlank(city)) {
                companyList = companyService.findByAreaCityName(city);
            }
            if (!CollectionUtils.isEmpty(companyList)) {

                if (null != passengerId) {
                    Passenger passenger = memberService.findOnePassenger(passengerId);
                    if (null != passenger) {
                        for (Company c : companyList) {
                            if (c.getId().equals(passenger.getCompanyId())) {
                                companyId = c.getId();
                                break;
                            }
                        }
                    }
                }

                List<Company> notMainCompanys = new ArrayList<Company>();
                Company cityMainCompany = null;

                if (companyId == null) {
                    for (Company c : companyList) {
                        if (!c.isCompanyType()) {
                            if (StringUtils.isBlank(c.getAppKey())) {
                                notMainCompanys.add(c);
                            }
                        } else {
                            cityMainCompany = c;
                        }
                    }

                    if (!CollectionUtils.isEmpty(notMainCompanys)) {
                        companyId = notMainCompanys
                            .get(new Random().nextInt(notMainCompanys.size())).getId();
                    }
                }

                if (companyId == null) {
                    if (null != cityMainCompany) {
                        companyId = cityMainCompany.getId();
                    }
                }
            }
        }
        if (companyId == null) {
            companyId = companyService.getMain().getId();
        }

        Page<ActivityManage> activityManages = activityManageService
            .queryByCompany(companyId, page, limit);

        map.put("data", activityManages);
        map.put("success", true);

        return map;

    }

    @RequestMapping(value = "indexActivityManages")
    public @ResponseBody
    Map<String, Object> indexActivityManages(
        @RequestParam(required = true, value = "longitude") Double longitude,
        @RequestParam(required = true, value = "latitude") Double latitude,
        @RequestParam(required = true, value = "serviceType") String serviceType,
        @RequestParam(required = false, value = "acKey") String acKey) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);
        Long companyId = null;
        if (StringUtils.isNotBlank(acKey)) {
            Company company = companyService.findByAppKey(acKey);
            if (company != null) {
                companyId = company.getId();
            }
        } else if (longitude != null && latitude != null) {
            String city = HttpClientUtil.getCityByLngAndLat(longitude, latitude);
            List<Company> companyList = null;
            if (StringUtils.isNotBlank(city)) {
                companyList = companyService.findByAreaCityName(city);
            }
            if (!CollectionUtils.isEmpty(companyList)) {

                List<Company> notMainCompanys = new ArrayList<Company>();
                Company cityMainCompany = null;

                for (Company c : companyList) {
                    if (!c.isCompanyType()) {
                        if (StringUtils.isBlank(c.getAppKey())) {
                            notMainCompanys.add(c);
                        }
                    } else {
                        cityMainCompany = c;
                    }
                }
                if (!CollectionUtils.isEmpty(notMainCompanys)) {
                    companyId = notMainCompanys.get(new Random().nextInt(notMainCompanys.size()))
                        .getId();
                }

                if (companyId == null) {
                    if (null != cityMainCompany) {
                        companyId = cityMainCompany.getId();
                    }
                }
            }
        }
        if (companyId == null) {
            companyId = companyService.getMain().getId();
        }

        List<ActivityManage> activityManages = activityManageService
            .queryIndex(companyId, ServiceType.valueOf(serviceType));

        map.put("data", activityManages);
        map.put("success", true);

        return map;

    }


}
